import {
  getAnalysisTable
  // editCustomerRecord
} from '../../api/sale'
import {
  ANALYSIS_LIST,
  ANALYSIS_CURRENT_ROW,
  ANALYSIS_DATA_COUNT,
  ANALYSIS_HISTORY_DATA,
  ANALYSIS_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    analysisList: [],
    analysisDataCount: null,
    analysisPageSize: 20,
    analysisHistoryData: [],
    row: {
      uid: '',
      ctime: '',
      uname: '',
      tel: '',
      coludname: '',
      coludtel: '',
      mname: '',
      appointime: '',
      state: '',
      comdate: '',
      tname: '',
      gid: '',
      gname: '',
      orderdate: '',
      cprice: '',
      commission: '',
      state0: '',
      ttype: '',
      remarks: '',
      perrate: '',
      permoney: '',
      budget: ''
    }
  },
  getters: {
    analysisList: state => state.analysisList
  },
  mutations: {
    [ANALYSIS_LIST] (state, data) {
      state.analysisList = data
    },
    [ANALYSIS_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [ANALYSIS_DATA_COUNT] (state, data) {
      state.analysisDataCount = data
    },
    [ANALYSIS_HISTORY_DATA] (state, data) {
      state.analysisHistoryData = data
    },
    [ANALYSIS_PAGE_SIZE] (state, data) {
      state.analysisPageSize = data
    }
  },
  actions: {
    async getAnalysisList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getAnalysisTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        // if ((item.state === 3 || item.state === 4 || item.state === 5 || item.state === 6) && item.state0 === 0) {
        newData = [...newData, {
          id: item.id,
          uid: item.uid,
          ctime: item.ctime,
          uname: item.uname,
          tel: item.tel,
          coludname: item.coludname,
          coludtel: item.coludtel,
          mname: item.mname,
          appointime: item.appointime,
          state: item.state.toString(),
          comdate: item.comdate,
          tname: item.tname,
          gid: item.gid,
          gname: item.gname,
          orderdate: item.orderdate,
          cprice: item.cprice,
          commission: item.commission,
          state0: item.state0,
          ttype: item.ttype ? item.ttype.toString() : '',
          remarks: item.remarks,
          perrate: item.perrate,
          permoney: item.permoney,
          budget: item.budget
        }]
        // }
      })
      commit(ANALYSIS_LIST, newData)
      commit(ANALYSIS_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.analysis.analysisPageSize) {
        commit(ANALYSIS_HISTORY_DATA, newData)
      } else {
        commit(ANALYSIS_HISTORY_DATA, newData.slice(0, rootState.analysis.analysisPageSize))
      }
      return resp
    },
    async editAnalysisData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      await editCustomerRecord(data)
    },
    async getAnalysisCurrentRow ({ commit, rootState }, row) {
      commit(ARRIVE_CURRENT_ROW, row)
    },
    async changeAnalysisPage ({ commit, rootState }, index) {
      let data = rootState.analysis.analysisList
      let _start = (index - 1) * rootState.analysis.analysisPageSize
      let _end = index * rootState.analysis.analysisPageSize
      commit(ANALYSIS_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeAnalysisPageSize ({ commit, rootState }, value) {
      commit(ANALYSIS_PAGE_SIZE, value)
    }
  }
}
